﻿#include "dxstdafx.h"
#include "LandedTitlesData.h"
#include "Data/DataMgr.h"
#include "Data/ScriptData/ScriptDataMgr.h"
#include "Data/ScriptData/LandedTitles/LandedTitles.h"
#include "ProvinceData.h"

_stLandedTitlesData::_stLandedTitlesData(const _stLandedTitlesProperty* p)
{
	if (p && !p->empty())
	{
		m_pLandedTitlesProperty = p;
		m_wFlagPath = m_wFlagPath + p->GetIndexName() + L".tga";
	}
	else
	{
		abort();
	}
}

std::wstring _stLandedTitlesData::GetDisplayName()
{
	if (!m_wstr_DisplayName.empty())
	{
		return m_wstr_DisplayName;
	}
	if (nullptr == m_pLandedTitlesProperty)
	{
		return L"";
	}
	size_t nFirstFind_Pos = m_pLandedTitlesProperty->GetIndexName().find(L"_") + 1;
	m_wstr_DisplayName = m_pLandedTitlesProperty->GetIndexName().substr(nFirstFind_Pos, m_pLandedTitlesProperty->GetIndexName().npos - nFirstFind_Pos);
	return m_wstr_DisplayName;
}

std::wstring _stLandedTitlesData::GetTitleslevelDisplayName() const
{
	return c_TitleLevelEnum2String[(int)m_pLandedTitlesProperty->m_eTitleslevel];
}

void CLandedTitlesData::LinkWithProvinceData(_stProvinceInfo* pCountyPalatine)
{
	if (nullptr == pCountyPalatine || !pCountyPalatine->IsAvailable())
	{
		return;
	}
	std::wstring wstrProvinceNameTransform = pCountyPalatine->IndexName;		// 全小写 + 用_替换空格

	transform(wstrProvinceNameTransform.begin(), wstrProvinceNameTransform.end(), wstrProvinceNameTransform.begin(), ::tolower);

	size_t findpos = wstrProvinceNameTransform.find(L" ");
	while (findpos != wstrProvinceNameTransform.npos)
	{
		wstrProvinceNameTransform = wstrProvinceNameTransform.replace(findpos, 1, L"_");
		findpos = wstrProvinceNameTransform.find(L" ", findpos);
	}

	wstrProvinceNameTransform = L"c_" + wstrProvinceNameTransform;

	auto itFind = m_mapLandedTitlesData.find(wstrProvinceNameTransform);
	if (itFind != m_mapLandedTitlesData.end())
	{
		itFind->second.m_pCountyPalatine = pCountyPalatine;
		pCountyPalatine->pLandedTitlesData = &itFind->second;
	}
}

void CLandedTitlesData::CreateDataAfterLandedTitlesPropertyRead()
{
	const CLandedTitlesProperty* cpLandedTitlesProperty = CDataMgr::Singleton().GetScriptDataMgrCPtr()->GetLandedTitlesPropertyCPtr();
	for (const auto& iter : cpLandedTitlesProperty->m_mapLandedTitlesProperty)
	{
		// 找头结点遍历
		if (!iter.second.empty() && nullptr == iter.second.m_pFatherTitle)
		{
			_stLandedTitlesData st(&iter.second);
			std::wstring wKey = iter.second.GetIndexName();
			auto iterInsert = m_mapLandedTitlesData.insert(std::make_pair(wKey, st));
			if (!iterInsert.second)
			{
				CString strError;
				strError.Format(L"'%s' is repeat!", wKey.c_str());
				CommonFunc::ErrorMessageBox(strError);
				return;
			}

			RecursiveChildNode(&iterInsert.first->second);
		}
	}
}

void CLandedTitlesData::RecursiveChildNode(_stLandedTitlesData* pFather)
{
	if (pFather && pFather->m_pLandedTitlesProperty && !pFather->m_pLandedTitlesProperty->empty())
	{
		for (const auto& iter : pFather->m_pLandedTitlesProperty->m_listChildTitles)
		{
			if (iter)
			{
				_stLandedTitlesData st(iter);
				st.m_pDeJureFather = const_cast<_stLandedTitlesData*>(pFather);
				std::wstring wKey = iter->GetIndexName();
				auto iterInsert = m_mapLandedTitlesData.insert(std::make_pair(wKey, st));
				if (!iterInsert.second)
				{
					CString strError;
					strError.Format(L"'%s' is repeat!", wKey.c_str());
					CommonFunc::ErrorMessageBox(strError);
					return;
				}
 				auto iterChildInsert = pFather->m_mapDeJureChild.insert(std::make_pair(wKey, &iterInsert.first->second));
				if (!iterChildInsert.second)
				{
					CString strError;
					strError.Format(L"'%s' is repeat!", wKey.c_str());
					CommonFunc::ErrorMessageBox(strError);
					return;
				}
				RecursiveChildNode(&iterInsert.first->second);
			}
		}
	}
}